<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      <a-radio-group v-model="Status" button-style="solid" @change="getDataList">
        <a-radio-button :value="undefined">全部</a-radio-button>
        <!--        <a-radio-button :value="3">已确认</a-radio-button>-->
        <a-radio-button :value="4">部分签收</a-radio-button>
        <a-radio-button :value="5">已签收</a-radio-button>
      </a-radio-group>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="6" :sm="24">
            <a-form-item>
              <a-input v-model="Name" placeholder="订单编号\采购订单名称\供货单位名称" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="react">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 560 }"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, columnWidth: '4%' }"
      :bordered="true"
      size="small"
    >
      <template slot="ExamineStatus" slot-scope="text, record, index">
        {{ ExamineStatus(text) }}
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <!-- 除了初始状态其他都能查看 -->

          <span v-if="record.Status != 1">
            <a @click="handleEdit(record.Id, record, '查看')">查看</a>
          </span>
          <!-- <span v-if="record.Status == 3">
            <a-divider type="vertical" v-if="hasPerm('PB_PurchaseAOG.Print')" />
            <a @click="handleprint(record.Id, record, '打码')">打码</a>
          </span> -->
          <!-- <span v-if="record.Status == 2 || record.Status == 4">
            <a-divider type="vertical" v-if="hasPerm('PB_PurchaseAOG.SignIn')" />
            <a @click="handleEdit(record.Id, record, '签收')" v-if="hasPerm('PB_PurchaseAOG.SignIn')">签收</a>
          </span> -->
        </template>
      </span>
      <template slot="dataIndex" slot-scope="text, record, index">
        <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
      </template>
    </a-table>
    <EditForm :parentObj="this" ref="editForm"></EditForm>
    <SingFor :parentObj="this" ref="SingFor"></SingFor>

    <print ref="print"></print>
  </a-card>
</template>

<script>
import SingFor from './SingFor'

import EditForm from './EditForm'
import moment from 'moment'
import print from './print.vue'
const filterDate = (value, row, index) => {
  if (value) {
    return moment(value).format('YYYY-MM-DD')
  } else {
    return ' '
  }
}
const Statusnum = (value, row, index) => {
  if (value == 1) {
    return '待审核'
  } else if (value == 2) {
    return '已审核'
    // } else if (value == 3) {
    //   return '已确认'
  } else if (value == 4) {
    return '部分签收'
  } else if (value == 5) {
    return '已签收'
  }
}

const columns = [
  {
    title: '序号',
    align: 'center',
    width: '5%',
    scopedSlots: { customRender: 'dataIndex' }
  },

  { title: '订单编号', dataIndex: 'Code', ellipsis: true },
  { title: '采购订单名称', dataIndex: 'Name', ellipsis: true },
  { title: '供货单位名称', dataIndex: 'SuppName', ellipsis: true },
  { title: '采购类型', dataIndex: 'TypeName', ellipsis: true },
  { title: '采购日期', dataIndex: 'PurchaseDate', ellipsis: true, customRender: filterDate },
  { title: '预计交货日期', dataIndex: 'HandoverDate', ellipsis: true, customRender: filterDate },

  { title: '状态', dataIndex: 'Status', ellipsis: true, customRender: Statusnum },
  { title: '制单人', dataIndex: 'CreatorRealName', ellipsis: true },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, align: 'left' }
]

export default {
  components: {
    EditForm,
    print,
    SingFor
  },
  created() {
    this.getDataList()
  },
  data() {
    return {
      visibleModal: false,
      data: [],
      pagination: {
        showSizeChanger: true,
        pageSizeOptions: ['25', '50', '100'],
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        defaultCurrent: 1,
        onChange: this.onPageChange.bind(this)
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: [],
      selectedRowKeys: [],
      Status: undefined,
      Name: ''
    }
  },
  methods: {
    // handleSub(id, row, tit) {
    //   this.$confirm({
    //     title: '确认签收吗?',
    //     onOk: () => {
    //       this.$http.post('/PB/PB_Purchase/UpdatePurchaseData?PurchaseId=' + id, {}).then(resJson => {
    //         if (resJson.Success) {
    //           this.$message.success('签收成功!')

    //           this.getDataList()
    //         } else {
    //           this.$message.error(resJson.Msg)
    //         }
    //       })
    //     }
    //   })
    // },
    handleprint(id, row, tit) {
      this.$refs.print.openForm(id, tit)
    },
    handleEdit(id, row, tit) {
      //审核 查看 编辑
      this.$refs.SingFor.openForm(id, tit)
    },
    react() {
      this.Status = undefined
      this.Name = ''
      this.queryParam = []
      this.pagination.current = 1
      this.pagination.pageSize = 25
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    onPageChange(current) {
      this.pagination.current = current
    },
    getDataList() {
      this.queryParam = [{ Condition: 'Status', Keyword: this.Status }, {
        Condition: 'Name',
        Keyword: this.Name
      }]
      this.loading = true
      this.$http
        .post('/PB/pb_purchase/GetPurchaseArrivalDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: 'CreateTime',
          SortType: 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      this.$refs.editForm.openForm(null, '新建')
    },
    handleDelete(ids) {
      this.$confirm({
        title: '确认删除吗?',
        onOk: () => {
          this.$http.post('/PB/PB_Purchase/DeleteData', ids).then(resJson => {
            if (resJson.Success) {
              this.$message.success('操作成功!')

              this.getDataList()
            } else {
              this.$message.error(resJson.Msg)
            }
          })
        }
      })
    }
  }
}
</script>
